package com.cloudfun.campusshare.device.protocol.decoder;

import com.cloudfun.campusshare.util.ByteUtil;

import java.util.Map;
import java.util.function.BiConsumer;

/**
 * Created by Huqin on 2020/1/3
 */
public class EnumFieldDecoder<T, V> extends SingleValueFieldDecoder<T, V> {

    private final Map<String, V> valMap;

    public EnumFieldDecoder(int start, int length, BiConsumer<T, V> fieldSetter, Map<String, V> valMap) {
        super(start, length, fieldSetter);
        this.valMap = valMap;
    }

    @Override
    public void decode(byte[] data, T obj) {
        String val = ByteUtil.bytesToHexString(data);
        getFieldSetter().accept(obj, valMap.get(val));
    }
}
